// miniprogram/pages/mycard/mycard.js
const app = getApp();
const db = wx.cloud.database();
const two = db.collection('two')
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    words: [],
    selected: 0,
    tochange: true,
    oneprice:true,
    towPrice:true,
    threePrice:true,
    avPrice:true
  },
  toindex() {
    wx.navigateBack({
      delta:10
    })

  },
  tomessage() {
    wx.navigateTo({
      url: '../message/message',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    two.where({
      '_openid': app.globalData._openid
    }).get().then((res) => {

      if (res.data.length > 0) {
        let words = res.data[0].words;
        this.setData({
          words: words
        })
        console.log('mycard', words)
      }
    })
  },
  select(e) {
    // 下标
    let index = e.currentTarget.dataset.index;
    // 获取data中words
    let {
      words,
      selected,
      tochange
    } = this.data;

    // 必须number为0时才可点击
    if (words[index].number == 0) {
      return;
    }

    // 只能选中两张不同的邮，
    if (selected >= 2 && !words[index].check) {
      return
    }
    if (!words[index].check) {
      // 未选中
      words[index].check = true;
      selected++;

    } else {
      // 选中
      words[index].check = false;
      selected--;
    }

    // 最终设置
    this.setData({
      words: words,
      selected: selected,
    })
  },
  // 合成邮
  merge() {
    // selected 必须为2
    let {
      selected,
      words,
      tochange,
      oneprice,
      towPrice,
      threePrice,
      avPrice
    } = this.data;

    if (selected < 2) {
      wx.showToast({
        title: '必须选择两个邮',
        icon: 'none',
        duration: 1500

      })
      return;
    }
    let selectedArr = []; //存放用户选择的下标
    words.forEach((item, index) => {
      if (item.check == true) {
        selectedArr.push(index)

      }
    })
    console.log('选中的下标', selectedArr)
    wx.cloud.callFunction({
      name: 'merge',
      data: {
        selectedArr: selectedArr
      },
      success: (res) => {
        console.log('跑进来', res)

        if (res.result.success) {
          let prize = res.result.prize
          console.log(res.result.prize)
          wx.showToast({
            title: '恭喜你抽到了' + prize + '等奖',
            icon: 'none',
            duration: 1500,
          })
          // 中奖界面渲染  hideen改为false
          tochange = false;

          // 中奖等级判断，相应渲染中奖等级
           if(prize==0){
              avPrice=false
           }
           if(prize==3){
             threePrice=false
           }
           if(prize==2){
             towPrice=false
           }
           if(prize==1){
             oneprice=false
           }
         
          this.setData({
            words: res.result.words,
            tochange: tochange,
            oneprice:oneprice,
            towPrice:towPrice,
            threePrice:threePrice,
            avPrice:avPrice
          })
        } else {
          wx.showToast({
            title: res.result.msg,
            icon: 'none',
            duration: 1500
          })
        }


      }
    })

  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})